/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

using System;

using TypeElement = argot.TypeElement;
using TypeException = argot.TypeException;
using TypeInputStream = argot.TypeInputStream;
using TypeOutputStream = argot.TypeOutputStream;
using TypeReader = argot.TypeReader;
using TypeReaderAuto = argot.TypeReaderAuto;
using TypeLibrary = argot.TypeLibrary;
using TypeWriter = argot.TypeWriter;

namespace argot.meta
{
	
	public class MetaSequence
	:MetaBase, TypeReader, TypeWriter, MetaExpression, MetaDefinition
	{
		
		public static System.String TYPENAME = "meta.sequence";
		
		private MetaExpression[] _objects;
		
		public MetaSequence(System.Object[] objects)
		{
			_objects = new MetaExpression[objects.Length];
			
			for (int x = 0; x < objects.Length; x++)
			{

				if (objects[x] is MetaExpression)
				{
					_objects[x] = (MetaExpression) objects[x];
				}
			}
		}
		
		public MetaSequence(System.Collections.IList objects)
		{
			_objects = new MetaExpression[objects.Count];
			
			for (int x = 0; x < objects.Count; x++)
			{
				System.Object val = objects[x];
				_objects[x] = (MetaExpression) val;
			}
		}
		
		public MetaSequence(MetaExpression[] objects)
		{
			_objects = objects;
		}

		override public System.String TypeName
		{
			get
			{
				return TYPENAME;
			}
			
		}
		
		public override void Bind(TypeLibrary library, TypeElement definition, System.String typeName, int typeId)
		{
			base.Bind(library, definition, typeName, typeId);
			for (int x = 0; x < _objects.Length; x++)
			{
				_objects[x].Bind(library, definition, typeName, typeId);
			}
		}
		public virtual MetaExpression GetElement(int x)
		{
			return (MetaExpression) _objects[x];
		}
		
		public virtual int Size()
		{
			return _objects.Length;
		}
		
		public virtual System.Object Read(TypeInputStream inStream, TypeElement element)
		{
			if (element is MetaExpression)
			{
				TypeReader reader = new TypeReaderAuto(this.GetType());
				return reader.Read(inStream, element);
			}
			throw new TypeException("shouldn't get here.");
		}
		
		public virtual void Write(TypeOutputStream outStream, System.Object obj, TypeElement element)
		{
			MetaSequence ts = (MetaSequence) obj;
			
			outStream.WriteObject("u8", (System.Object) ts._objects.Length);
			
			for (int x = 0; x < ts._objects.Length; x++)
			{
				System.Object o = ts._objects[x];
				outStream.WriteObject("meta.expression", o);
			}
		}

		public virtual System.Object DoRead(TypeInputStream inStream)
		{
			System.Object[] objects = new System.Object[Size()];
			
			for (int x = 0; x < Size(); x++)
			{
				
				MetaExpression te = GetElement(x);
				objects[x] = te.DoRead(inStream);
			}

			return objects;
		}
		
		public virtual void DoWrite(TypeOutputStream outStream, System.Object o)
		{
			throw new TypeException("Not Implemented");
		}
	}
}